

**************************************************************** REPLICATION DO FILE FOR "Demographic Polarization and the Rise of the Far Right: Brazil’s 2018 Presidential Election"
**************************************************************** ANONYMIZED FOR REVIEW

**** Note that this file only contains analysis to replicate our models in the Brazilian Democracy in the Balance dataset.  
**** set your directory path:
cd "XXX"

**** open dataset:
use "BDIB_R&P_replication_10.2.dta", clear


************************************************************ variable coding *************************************************************************************************************** 
tsset idnumber wave
recode cand_vote_2ndround (1=0) (2=1) (3/99 = .), g(bolso_2nd)
****** demographics
recode religion (1=1) (2 3 = 2) (3 4 5 6 7 8 9 = 4) (10 11 = 3), g(relig2)
	lab def relig2 1 "Catholic" 2 "Evangelical" 3 "None" 4 "Other"
	lab val relig2 relig2
	tab relig2, g(rel)
recode relig2 (4=3), g(relig3)
recode etid (1=1) (2=4) (4=2) (3=3) (5 6=5) 
	lab def etid 1 "White" 2 "Brown" 3 "Black" 4 "Indigenous" 5 "Asian/Other"
	lab val etid etid
	replace etid = l2.etid if wave == 3
	replace etid = l3.etid if wave == 4
	tab etid, g(etid)
recode ed2_1 (1 2 3 = 1) (4 5 6 7 = 2) (8 9 10 = 3), g(ed)
	lab def ed 1 "None/Primary" 2 "Secondary" 3 "Higher" 
	lab val ed ed
	replace ed = l2.ed if wave == 3
	replace ed = l3.ed if wave == 4
recode ed (3=1) (1 2 = 0), g(highered)
recode q2 (16/25 = 1) (26/35 = 2) (36/45 = 3) (46/55 = 4) (56/65 = 5) (66/100=6), g(edad)
	lab def edad 1 "Age 16-25" 2 "Age 26-35" 3 "Age 36-45" 4 "Age 46-55" 5 "Age 56-65" 6 "Age 66/100"
	lab val edad edad
egen ses = mean(q10new), by(idnumber)
	replace ses = (ses-1)/16
	g ses2 = (6-netquest_socialclass)/5
	replace ses = ses2 if ses == .
recode q10new (1 2 3 = 1) (4 5 6 7 8 9 = 2) (10/15 = 3) (16/17 = 4), g(income)
	lab def income 1 "<= R$950" 2 "R$951-1950" 3 "R$1951-4950" 4 "R$4951+"
	lab val income income	
tab netquest_regions, gen(region)
***** issues
replace issues_arms = f2.issues_arms  if wave == 1
	replace issues_arms = l.issues_arms if wave == 4
replace issues_racialresent = f2.issues_racialresent  if wave == 1
	replace issues_racialresent = l.issues_racialresent  if wave == 4
replace group_attitudes_homo = f2.group_attitudes_homo if wave == 1
	replace group_attitudes_homo = l.group_attitudes_homo if wave == 4
egen bf_stereotypes = rowmean(brawrs*)
recode humrts (1=1) (2 3 = 0), g(goodcriminal)
foreach indvar in issues_gender issues_arms issues_racialresent group_attitudes_quil group_attitudes_homoss brawrs1 brawrs2 brawrs3 {
	replace `indvar' = (`indvar'-1)/6
}
***** evaluations
g petista = vb11 == 1 if wave == 1
	egen petistaw1 = mean(petista), by(idnumber)
g lulatherm = cand_series_lula if wave == 1
	replace lulatherm = (lulatherm-1)/6
	egen lulathermw1 = mean(lulatherm), by(idnumber)
*egen petismo = rowmean(petistaw1 lulathermw1)
egen petismo = rowmean(petistaw1 lulathermw1) if lulathermw1 < . & petistaw1 < .
replace exc7new = (exc7new-1)/4
gen corruptpol = exc7new if wave==3
	replace corruptpol = f2.corruptpol if wave == 1
	replace corruptpol = l.corruptpol if wave == 4
recode soct2 (3=0) (2=0.5), g(sociotropic)
	replace sociotropic = f2.sociotropic if wave == 1
recode idio2 (3=0) (2=0.5), g(idiotropic)
	replace idiotropic = f2.idiotropic if wave == 1
	



************************************* Figure 1: 2018 Demographic Effects ************************************************************
global demographics rel2 rel1 etid3 etid2 mulher 
tempfile $demographics 
foreach var in $demographics {
	quietly logit bolso_2nd mulher etid2 etid3 etid4 etid5 rel1 rel2 rel4 ses highered i.netquest_regions i.edad 
	margins, dydx(`var') post level(95)
	parmest, saving(``var'', replace) level(95)
}
preserve
dsconcat `rel2' `rel1' `etid3' `etid2' `mulher' 
sencode parm, replace
	lab def parm 5 "Female (v. Male)" 4 "Brown (v. White)" 3 "Black (v. White)" 2 "Catholic (v. No Religion)" 1 "Evangelical (v. No Religion)" ///
		, modify
	lab val parm parm
sort parm
rename parm yvar
graph twoway (rspike min95 max95 yvar, horizontal lcolor(gs10)) ///
			 (scatter yvar estimate, msymbol(T) msize(large) mcolor(black)), ///
		graphregion(fcolor(white) lcolor(black)) legend(off) ///
		ylabel(1(1)5, valuelabel angle(horizontal) notick nogrid) yscale(r(0.5 5.5)) ///
		xline(0, lcolor(gs10)) ytitle("") xlabel(-.4(.2).4, grid) ///
		xtitle("Impact of Variable on Change in Predicted Probability of" "Supporting Bolsonaro in the Second Round", margin(small)) 
restore
************************************* END Figure 1: 2018 Demographic Effects **********************************************************


************************************* Table: Multivariate models *******************************************************************************
eststo clear
global demographics mulher i.etid b3.relig2 b2.ed ses 
global performance petismo corruptpol l.sociotropic l.idiotropic
global attitudes l.issues_arms l.issues_racialresent l.group_attitudes_homoss 
logit bolso_2nd $demographics i.netquest_regions i.edad
	eststo
logit bolso_2nd $demographics $performance i.netquest_regions i.edad
	eststo
logit bolso_2nd $demographics $performance $attitudes i.netquest_regions i.edad
	eststo
esttab, b(3) se(3) star(^ .10 * .05 ** .01) nogaps nopar
************************************* END Table: Multivariate models ****************************************************************************


************ APPENDIX TABLE 1: SAMPLE STATS from our data ****************************************************************************************
tab1 wave // N in wave 1
count if wave == 1 & (responsepattern == 5 | responsepattern == 6 | responsepattern == 12 | responsepattern == 13) //N in waves 1, 3, and 4
count if wave == 1 & f2.wave == 3 & f3.wave == 4 // same result, fortunately
	g waves134 = (wave == 1 & f2.wave == 3 & f3.wave == 4 ) //indicator for this sample
foreach x in wave waves134 {
	foreach indvar in mulher etid relig2 netquest_regions ed income edad {
		tab1 `indvar' if `x' == 1
	}
}
tab1 bolso_2nd if l3.waves134 == 1
*************************************************************************************************************************************************


************************************* Appendix: Differences of means on attitudes ***********************************************************************
foreach dv in petismo corruptpol issues_arms issues_racialresent group_attitudes_homoss {
	foreach indvar in mulher etid relig3 {
		preserve
			tempfile `dv'_`indvar'
			quietly reg `dv' i.`indvar' i.ed i.netquest_regions netquest_socialclass i.edad if wave == 1
			quietly margins i.`indvar', post
			parmest, saving(``dv'_`indvar'', replace) idstr(`indvar') 
		restore
	}
	preserve
		tempfile results_`dv'
		dsconcat ``dv'_mulher' ``dv'_etid' ``dv'_relig3'
		g dv = "`dv'"
		save `results_`dv'', replace
	restore
}
foreach dv in sociotropic idiotropic {
	foreach indvar in mulher etid relig3 {
		preserve
			tempfile `dv'_`indvar' 
			quietly logit `dv' i.`indvar' i.ed i.netquest_regions netquest_socialclass i.edad if wave == 1
			quietly margins i.`indvar', post
			parmest, saving(``dv'_`indvar'', replace) idstr(`indvar') 
		restore
	}
	preserve
		tempfile results_`dv'
		dsconcat ``dv'_mulher' ``dv'_etid' ``dv'_relig3' 
		g dv = "`dv'"
		save `results_`dv'', replace
	restore
}
preserve
clear
foreach dv in petismo sociotropic idiotropic corruptpol issues_arms issues_racialresent group_attitudes_homoss  {
	append using `results_`dv''
}	
drop if parm == "4.etid" | parm == "5.etid"
sencode parm, replace
	lab def parm 1 "Male" 2 "Female" 3 "White" 4 "Brown" 5 "Black" 6 "Catholic" 7 "Evangelical" 8 "Other/None", replace
	lab val parm parm
sencode dv, replace
	lab def dv 1 "Petismo" 2 "Sociotropic Eval" 3 "Idiotropic Eval" 4 "Corruption Eval" 5 "Right to Bear Arms" 6 "Racial Resentment" 7 "Feeling Thermometer: LGBT", modify 
	lab val dv dv
gsort idstr dv -parm
g xvar = _n if idstr == "etid"
	replace xvar = _n-21 if idstr == "mulher"
	replace xvar = _n-35 if idstr == "relig3"
graph twoway (rspike min95 max95 xvar if idstr == "etid", lcolor(gs10)) ///
			 (scatter estimate xvar if parm == 3 & idstr == "etid", msymbol(T) mcolor(black)) ///
			 (scatter estimate xvar if parm == 4 & idstr == "etid", msymbol(S) mcolor(gs8)) ///
			 (scatter estimate xvar if parm == 5 & idstr == "etid", msymbol(Oh) mcolor(black)), ///
	graphregion(fcolor(white) lcolor(black)) plotregion(lcolor(black)) ///
	xlabel(2 "Petismo" 5 "Sociotropic Eval" 8 "Idiotropic Eval" 11 "Corruption Eval" 14 "Right to Bear Arms" 17 "Racial Resentment" 20 "LGBT Thermometer" ///
				, angle(horizontal) nogrid notick labsize(small)) xtitle("") ///
	xline(3.5, lcolor(gs12) lwidth(thin)) xline(6.5, lcolor(gs12) lwidth(thin)) xline(9.5, lcolor(gs12) lwidth(thin)) xline(12.5, lcolor(gs12) lwidth(thin)) ///
			xline(15.5, lcolor(gs12) lwidth(thin)) xline(18.5, lcolor(gs12) lwidth(thin)) ///
	ytitle("Predicted Mean Value", margin(small)) ylabel(0(.2)1) legend(order(4 "Black" 3 "Brown" 2 "White" ) col(3) span) ///
	name(etidfigure, replace)
graph twoway (rspike min95 max95 xvar if idstr == "mulher", lcolor(gs10)) ///
			 (scatter estimate xvar if parm == 1 & idstr == "mulher", msymbol(T) mcolor(black)) ///
			 (scatter estimate xvar if parm == 2 & idstr == "mulher", msymbol(S) mcolor(gs8)), ///
	graphregion(fcolor(white) lcolor(black)) plotregion(lcolor(black)) ///
	xlabel(1.5 "Petismo" 3.5 "Sociotropic Eval" 5.5 "Idiotropic Eval" 7.5 "Corruption Eval" 9.5 "Right to Bear Arms" 11.5 "Racial Resentment" 13.5 "Feeling Thermometer: LGBT" ///
				, angle(horizontal) nogrid notick labsize(small)) xtitle("") ///
	ytitle("Predicted Mean Value", margin(small)) ylabel(0(.2)1) legend(order(3 "Female" 2 "Male") col(2) span) xtitle("") ///
	xline(2.5, lcolor(gs12) lwidth(thin)) xline(4.5, lcolor(gs12) lwidth(thin)) xline(6.5, lcolor(gs12) lwidth(thin)) xline(8.5, lcolor(gs12) lwidth(thin)) ///
			xline(10.5, lcolor(gs12) lwidth(thin)) xline(12.5, lcolor(gs12) lwidth(thin)) ///
	name(genderfigure, replace)
graph twoway (rspike min95 max95 xvar if idstr == "relig3", lcolor(gs10)) ///
			 (scatter estimate xvar if parm == 6 & idstr == "relig3", msymbol(T) mcolor(black)) ///
			 (scatter estimate xvar if parm == 7 & idstr == "relig3", msymbol(S) mcolor(gs8)) ///
			 (scatter estimate xvar if parm == 8 & idstr == "relig3", msymbol(Oh) mcolor(black)), ///
	graphregion(fcolor(white) lcolor(black)) plotregion(lcolor(black)) ///
	xlabel(2 "Petismo" 5 "Sociotropic Eval" 8 "Idiotropic Eval" 11 "Corruption Eval" 14 "Right to Bear Arms" 17 "Racial Resentment" 20 "LGBT Thermometer" ///
				, angle(horizontal) nogrid notick labsize(small)) xtitle("") ///
	xline(3.5, lcolor(gs12) lwidth(thin)) xline(6.5, lcolor(gs12) lwidth(thin)) xline(9.5, lcolor(gs12) lwidth(thin)) xline(12.5, lcolor(gs12) lwidth(thin)) ///
			xline(15.5, lcolor(gs12) lwidth(thin)) xline(18.5, lcolor(gs12) lwidth(thin)) xline(21.5, lcolor(gs12) lwidth(thin)) ///
	ytitle("Predicted Mean Value", margin(small)) ylabel(0(.2)1) legend(order(4 "Other/" "None" 3 "Evangelical" 2 "Catholic" ) col(3) span) ///
	name(religfigure, replace)
restore
************************************* END Differences of means on attitudes ********************************************************************


************************************* MEDIATION MODELS - APPENDIX ****************************************************************************

*race
medeff(regress issues_racialresent etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2) (logit bolso_2nd issues_racialresent etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2), mediate(issues_racialresent) treat(etid3) sims(500) seed(1)
medeff(regress issues_racialresent etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2 issues_arms group_attitudes_homo) (logit bolso_2nd issues_racialresent etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2 issues_arms group_attitudes_homo), mediate(issues_racialresent) treat(etid3) sims(500) seed(1)
* gender
medeff(regress issues_arms etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2) (logit bolso_2nd issues_arms etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2), mediate(issues_arms) treat(mulher) sims(500) seed(1)
medeff(regress issues_arms etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2 issues_racialresent group_attitudes_homo) (logit bolso_2nd issues_arms etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2 issues_racialresent group_attitudes_homo), mediate(issues_arms) treat(mulher) sims(500) seed(1)
* LGBT
medeff(regress group_attitudes_homossexuais etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2) (logit bolso_2nd group_attitudes_homossexuais etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2), mediate(group_attitudes_homossexuais) treat(rel2) sims(500) seed(1)
medeff(regress group_attitudes_homossexuais etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2 issues_racialresent issues_arms) (logit bolso_2nd group_attitudes_homossexuais etid3 mulher rel2 ed edad region2 region3 region4 region5 ses2 issues_racialresent issues_arms), mediate(group_attitudes_homossexuais) treat(rel2) sims(500) seed(1)

************************************* END MEDIATION MODELS ****************************************************************************


************ APPENDIX Table xxx: Full models *********************************************************************************
eststo clear
global demographics mulher i.etid b3.relig2 b2.ed ses i.edad
global performance petistaw1 lulathermw1 exc7new l.sociotropic l.idiotropic
global attitudes l.issues_gender l.issues_arms l.issues_racialresent l.group_attitudes_quil l.goodcriminal l.group_attitudes_homoss l3.bf_stereotypes
logit bolso_2nd $demographics i.netquest_regions 
	eststo
logit bolso_2nd $demographics $performance i.netquest_regions 
	eststo
logit bolso_2nd $demographics $performance $attitudes i.netquest_regions 
	eststo
esttab, b(3) se(3) star(^ .10 * .05 ** .01) nogaps nopar 
************ END APPENDIX Table xxx: Full models *********************************************************************************


************ APPENDIX Table xxx: First-wave vote choice *********************************************************************************

recode cand_vote (2=1)(else=0), gen(firstwv_bols)
replace firstwv_bols=. if cand_vote==.

eststo clear
global demographics mulher i.etid b3.relig2 b2.ed ses i.edad 
global performance petismo exc7new l.sociotropic l.idiotropic 
global attitudes  l.issues_arms l.issues_racialresent l.group_attitudes_homoss 
logit bolso_2nd $demographics firstwv_bols i.netquest_regions 
	eststo
	outreg2 using "bolsofirst.doc", replace
logit bolso_2nd $demographics $performance firstwv_bols i.netquest_regions 
	eststo
	outreg2 using "bolsofirst.doc", append
logit bolso_2nd $demographics $performance $attitudes firstwv_bols i.netquest_regions 
	eststo
	outreg2 using "bolsofirst.doc", append
esttab, b(3) se(3) star(^ .10 * .05 ** .01) nogaps nopar 

************ END APPENDIX Table xxx: First-wave vote choice *********************************************************************************


************ APPENDIX Table xxx: Evangelicalism versus Religious *********************************************************************************

recode relig2(3=0)(1 2 4=1), gen(religious)
recode relig2 (2=1)(3=0)(else=.), gen(evanvsnone)

eststo clear
global demographics mulher i.etid b3.religious b2.ed ses i.edad 
global performance petismo exc7new l.sociotropic l.idiotropic 
global attitudes  l.issues_arms l.issues_racialresent l.group_attitudes_homoss 

logit bolso_2nd $demographics  i.netquest_regions 
predict yhat1
estat ic
logit bolso_2nd $demographics $performance  i.netquest_regions 
predict yhat2
estat ic
logit bolso_2nd $demographics $performance $attitudes  i.netquest_regions 
predict yhat3
estat ic

global demographics2 mulher i.etid b3.evanvsnone b2.ed ses i.edad 
global performance2 petismo exc7new l.sociotropic l.idiotropic 
global attitudes2  l.issues_arms l.issues_racialresent l.group_attitudes_homoss 

logit bolso_2nd $demographics2  i.netquest_regions 
predict yhat4
estat ic
logit bolso_2nd $demographics2 $performance2  i.netquest_regions 
predict yhat5
estat ic
logit bolso_2nd $demographics2 $performance2 $attitudes2  i.netquest_regions 
predict yhat6
estat ic

gen predbolso1=. 
gen predbolso2=. 
gen predbolso3=. 
gen predbolso4=. 
gen predbolso5=. 
gen predbolso6=. 

forvalues i = 1/6 {
replace predbolso`i'=0 if yhat`i'<.5
replace predbolso`i'=1 if yhat`i'>.5 & yhat`i'!=.
}

* the percent correctly predicted is in the fourth quadrant (both values are 1)
* PCP is the sum of diagonal values (0 and 0 + 1 and 1)

tab predbolso1 bolso_2, cell
tab predbolso2 bolso_2, cell
tab predbolso3 bolso_2, cell
tab predbolso4 bolso_2, cell
tab predbolso5 bolso_2, cell
tab predbolso6 bolso_2, cell

* r-square as a measure of fit

reg bolso_2nd $demographics  i.netquest_regions 
eststo
reg bolso_2nd $demographics $performance  i.netquest_regions 
eststo
reg bolso_2nd $demographics $performance $attitudes  i.netquest_regions 
eststo

reg bolso_2nd $demographics2  i.netquest_regions 
eststo
reg bolso_2nd $demographics2 $performance2  i.netquest_regions 
eststo
reg bolso_2nd $demographics2 $performance2 $attitudes2  i.netquest_regions 
eststo

esttab, r2

************ END APPENDIX Table xxx: Full models *********************************************************************************

